const path = require("path");
const Koa = require("koa");
const { koaBody } = require("koa-body");
const koaStatic = require("koa-static");
const cors = require("@koa/cors");

const userRouter = require("../router/admin/user.route");
const newsRouter = require("../router/admin/news.route");
const webNewsRouter = require("../router/web/news.route");
const productRouter = require("../router/admin/product.route");
const errHandler = require("./errHandler");

const app = new Koa();

app.use(cors({ exposeHeaders: "Authorization" }));
app.use(koaBody());
app.use(koaStatic(path.join(__dirname, "../public")));

app.use(userRouter.routes());
app.use(newsRouter.routes());
app.use(productRouter.routes());
app.use(webNewsRouter.routes());

//统一的错误处理
app.on("error", errHandler);

module.exports = app;
